/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parser.part.n2;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Patronus
 */
public class GoInXML {

    public boolean inFile(String adr, String xml) {
        PrintStream file = null;
        FileOutputStream fout;
        try {
            fout = new FileOutputStream(adr);
            file = new PrintStream(fout);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(GoInXML.class.getName()).log(Level.SEVERE, null, ex);
        }

        file.println(xml);
        return true;
    }

    public String getXML(List l) {
        String fileName = "D:\\out.xml";
        String root = "root";
        String rule = "rule";
        String begin = "<?xml version='1.0'?>";
        String arguments = "arguments";
        String importParametrs = "importParametrs";

        String args = "args";
        String imports = "import";
        String lines = "line";


        String xml = new String();
        Worker wrk = new Worker(l);
        xml += (begin);
        xml += (open(root));
        System.out.println("Size :" + l.size());
        int i = 0;

        for (Object hor : l) {//each line like List Object 
            if (hor.getClass().getCanonicalName().indexOf("List") != -1) {
                xml += (openWith(lines, "count", String.valueOf(i)));
                List line = (List) hor;
                for (Object o : line) {//all collumns
                    Ruler r = (Ruler) o;
                    {
                        xml += openWith(rule, "line", String.valueOf(i));
                        xml += (setValue("classname", r.getClass().getCanonicalName()));
                        xml += (setValue("id", r.getId()));
                        xml += (setValue("expression", r.getExpression()));
                        xml += (setValue("return", Boolean.toString(r.getReturnResult())));
//список аргументов 
                        xml += open(arguments);
                        {
                            Object[] ar = r.getArgs();
                            for (int k = 0; k < ar.length; k++) {

                                if (!String.valueOf(ar[k]).equals("null")) {
                                    xml += getFieldWithId(args, String.valueOf(k), String.valueOf(ar[k]));
                                }

                            }
                        }
                        xml += close(arguments);

//параметры импорта
                        xml += open(importParametrs);
                        {
                            Object[][] ip = r.getImport();

                            for (int t = 0; t < ip.length; t++) {
                                if (!String.valueOf(ip[t][0]).equals("null")) {
                                    xml += getFieldWithId(imports, String.valueOf(t), String.valueOf(ip[t][0]));
                                }
                            }
                        }
                        xml += close(importParametrs);


                    }
                    xml += (close(rule));
                }
                xml += (close(lines));
            }
            i++;
        }
        xml += (close(root));
        return xml;
    }

    public Object getTree(String s) {

        return new Object();
    }

    private String close(String s) {

        return "</" + s + ">";
    }

    private String open(String s) {

        return "<" + s + ">";
    }

    private String setValue(String name, String value) {
        return open(name) + value + close(name);
    }

    private String setOpen(String name, Map map) {
        String s = "";
        String spase = " ";
        Set set = map.entrySet();

        s = "<" + name + spase;
        Iterator iter = set.iterator();

        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            System.out.println(entry.getKey() + " -- " + entry.getValue());
        }
        return s;
    }

    private String getFieldWithId(String name, String id, String value) {
        String position = "position";
        String s = "";
        s += "<" + name + " " + position + "=" + "'" + id + "' value='"+value + "'/>";
            
        return s;
    }

    private String openWith(String name, String arg, String argValue) {
        return "<" + name + " " + arg + "='" + argValue + "'>";
    }
}
